#pragma once

#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include "log.hpp"

class Task
{
public:
    Task(int fd) : fd_(fd) {}
    
    void operator()()
    {
        char buf[1024];
        int n = recv(fd_,buf,1023,0);
        if(n <= 0){
            lg(Warning,"client quit");
            close(fd_);
            return;
        }

        lg(Info,"thread-%d, recv message: %s",pthread_self(),buf);
        
        std::string echo_string = "server echo@";
        echo_string += buf;
        n = send(fd_,echo_string.c_str(),echo_string.size(),0);
        if(n < 0){
            lg(Warning,"client quit");
            close(fd_);
            return;
        }

        //完成一次服务直接关闭连接
        close(fd_);
    }

private:
    int fd_;
};